package bite;

import java.util.Scanner;

/**
 * Creared with IntelliJ IDEA.
 * Description:链接：https://www.nowcoder.com/questionTerminal/f11155006f154419b0bef6de8918aea2
 * 来源：牛客网
 *
 * 给定一个单链表，在链表中把第 L 个节点到第 R 个节点这一部分进行反转。
 *
 * 输入描述:
 * n 表示单链表的长度。
 *
 * val 表示单链表各个节点的值。
 *
 * L 表示翻转区间的左端点。
 *
 * R 表示翻转区间的右端点。
 * User:yxd
 * Date:2022-05-25
 * Time:16:51
 */
public class Day28_1 {
    //使用静态内部类构造一个节点类

    static class Node{
        int val;
        Node next;
        public Node(int val) {
            this.val = val;
        }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        //使用nextLine吃掉nextInt的换行
        input.nextLine();
        String s = input.nextLine();
        //通过分割空格得到所有节点的值
        String[] arr = s.split(" ");
        //开始构造一个带头结点的链表
        Node newHead = new Node(-1);
        //头结点
        //将节点连起来
        Node cur = newHead;
        //遍历整个链表
        for (int i = 0; i < arr.length; i++) {
            cur.next = new Node(Integer.parseInt(arr[i]));
            cur = cur.next;
        }
        String str = input.nextLine();
        //接下来开始反转
        int left = Integer.parseInt(str.split(" ")[0]);
        int right = Integer.parseInt(str.split(" ")[1]);
        Node head = reserve(newHead.next,left,right);
        while(head.next != null){
            System.out.print(head.val + " ");
            head = head.next;
        }
        System.out.println(head.val);
    }

    public static Node reserve(Node head, int left, int right) {
        Node newHead = new Node(-1);
        newHead.next = head;

        Node prev = newHead;
        //走到要反转节点的头驱节点
        for(int i = 1;i < left;i ++){
            prev = prev.next;
        }
        //然后进行反转
        Node cur = prev.next;
        for (int i = left; i < right; i++) {
            //定义一个新节点表示要反转节点的下一个节点
            Node curNext = cur.next;
            cur.next = curNext.next;
            curNext.next = prev.next;
            prev.next = curNext;

        }
        return newHead.next;
    }

    public static void main1(String[] args){
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int[] nums = new int[n];
        for(int i = 0;i < n; i++){
            nums[i] = input.nextInt();
        }
        int left = input.nextInt();
        int right = input.nextInt();
        while(left < right){
            int tem = nums[left - 1];
            nums[left - 1] = nums[right - 1];
            nums[right - 1] = tem;
            left ++;
            right --;
        }
        for(int i = 0;i < n - 1;i ++){
            System.out.print(nums[i] + " ");
        }
        System.out.println(nums[n - 1]);
    }

}
